package com.lepu.app.fun.hospital.adapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.core.lib.utils.photo.UniversalImageLoadTool;
import com.core.lib.widget.CircularImageView;
import com.eyzhs.app.R;
import com.lepu.app.fun.hospital.bean.HospitalDoctor;

public class ExpertAdapter extends BaseAdapter {

	private LayoutInflater mLayoutInflater = null;
	@SuppressWarnings("unused")
	private Context mContext = null;
	private ArrayList<HospitalDoctor> mArrayList = null;

	@SuppressLint("UseSparseArrays")
	private Map<Integer, Object> mMaps = new HashMap<Integer, Object>();

	public ExpertAdapter(Context context, ArrayList<HospitalDoctor> arrayList) {
		mContext = context;
		mLayoutInflater = LayoutInflater.from(context);
		mArrayList = arrayList;
	}

	@Override
	public int getCount() {
		int size = 0;
		if (mArrayList != null && mArrayList.size() > 0) {
			size = mArrayList.size();
		}
		return size;
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@SuppressLint("InflateParams")
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		final ViewHolder holder;

		if (convertView == null) {
			convertView = mLayoutInflater.inflate(
					R.layout.hospital_expert_item, null);

			holder = new ViewHolder();
			ArrayList<TextView> goodTextViewList = new ArrayList<TextView>();

			CircularImageView iconImageView = (CircularImageView) convertView
					.findViewById(R.id.hospital_expert_icon);
			TextView titleTextView1 = (TextView) convertView
					.findViewById(R.id.hospital_expert_title1);
			TextView titleTextView2 = (TextView) convertView
					.findViewById(R.id.hospital_expert_title2);
			TextView contentTextView = (TextView) convertView
					.findViewById(R.id.hospital_expert_content);
			TextView goodTextView1 = (TextView) convertView
					.findViewById(R.id.hospital_expert_good1);
			TextView goodTextView2 = (TextView) convertView
					.findViewById(R.id.hospital_expert_good2);
			TextView goodTextView3 = (TextView) convertView
					.findViewById(R.id.hospital_expert_good3);
			TextView goodTextView4 = (TextView) convertView
					.findViewById(R.id.hospital_expert_good4);
			TextView goodTextView5 = (TextView) convertView
					.findViewById(R.id.hospital_expert_good5);
			Button lookmoreButton = (Button) convertView
					.findViewById(R.id.hospital_expert_lookmore);

			goodTextViewList.add(goodTextView1);
			goodTextViewList.add(goodTextView2);
			goodTextViewList.add(goodTextView3);
			goodTextViewList.add(goodTextView4);
			goodTextViewList.add(goodTextView5);

			holder.iconImageView = iconImageView;
			holder.titleTextView1 = titleTextView1;
			holder.titleTextView2 = titleTextView2;
			holder.contentTextView = contentTextView;
			holder.goodTextViewList = goodTextViewList;
			holder.lookmoreButton = lookmoreButton;

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
			//resetViewHolder(holder);
		}

		HospitalDoctor item = mArrayList.get(position);

		holder.titleTextView1.setText(item.DoctorName);
		holder.titleTextView2.setText(item.JobTitle);
		holder.contentTextView.setText(item.Resume);

		// if(holder.contentTextView.getLineCount() <= 5){
		// holder.lookmoreButton.setVisibility(View.GONE);
		// }else{
		// holder.contentTextView.setMaxLines(5);
		// holder.lookmoreButton.setVisibility(View.VISIBLE);
		// }

		String[] skillArray = item.Skill.split("\\|");
		for (int i = 0; i < skillArray.length; i++) {
			if (i < holder.goodTextViewList.size()) {
				holder.goodTextViewList.get(i).setText(skillArray[i]);
			}
		}

		// holder.iconImageView.setImageBitmap(ImageUtilBase.getBitmapByResId(
		// mContext, R.drawable.default_icon_small));
		// // 加载图片
		// if (!TextUtils.isEmpty(item.Avatar)) {
		// // 缓存到本地
		// String fileName = EncodeMD5.getMd5(item.Avatar);
		// String filePath = SdLocal.getCacheImagePath(mContext, fileName);
		// String imageUrl = item.Avatar;
		// BitmapManager.getInstance().loadBitmap(imageUrl, filePath,
		// holder.iconImageView, true);
		// }

		UniversalImageLoadTool.disPlay(item.Avatar, holder.iconImageView,
				R.drawable.default_icon_small);

		if (item.Resume.length() > 60) {
			holder.lookmoreButton.setVisibility(View.VISIBLE);

			if (mMaps.containsKey(position)) {
				holder.contentTextView.setEllipsize(null); // 展开
				holder.contentTextView.setSingleLine(false);
				holder.lookmoreButton.setText("收起详情");
				holder.lookmoreButton.setTag(1);
			} else {
				holder.contentTextView.setEllipsize(TextUtils.TruncateAt.END); // 收缩
				holder.contentTextView.setLines(3);
				holder.lookmoreButton.setText("查看更多");
				holder.lookmoreButton.setTag(0);
			}
			
			holder.lookmoreButton
					.setOnClickListener(new View.OnClickListener() {


						@Override
						public void onClick(View v) {

							int tag = (Integer) v.getTag();
							if (tag == 0) {
								holder.contentTextView.setEllipsize(null); // 展开
								holder.contentTextView.setSingleLine(false);
								holder.lookmoreButton.setText("收起详情");
								holder.lookmoreButton.setTag(1);

								mMaps.put(position, "");

							} else {
								holder.contentTextView
										.setEllipsize(TextUtils.TruncateAt.END); // 收缩
								holder.contentTextView.setLines(3);
								holder.lookmoreButton.setText("查看更多");
								holder.lookmoreButton.setTag(0);

								mMaps.remove(position);
							}
						}
					});
		} else {
			holder.lookmoreButton.setVisibility(View.GONE);
		}

		return convertView;
	}

	class MyViewOnclicklistener implements View.OnClickListener {

		TextView textView;

		public MyViewOnclicklistener(TextView textView) {
			this.textView = textView;
		}

		@Override
		public void onClick(View v) {
			textView.setMaxLines(Integer.MAX_VALUE);
		}
	}

//	private void resetViewHolder(ViewHolder holder) {
//		holder.contentTextView.setEllipsize(TextUtils.TruncateAt.END); // 收缩
//		holder.contentTextView.setLines(3);
//		holder.lookmoreButton.setText("查看更多");
//	}

	class ViewHolder {
		CircularImageView iconImageView;
		TextView titleTextView1;
		TextView titleTextView2;
		TextView contentTextView;
		ArrayList<TextView> goodTextViewList;
		Button lookmoreButton;
	}

}
